package com.xiongjie;

import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSSigner;
import com.qcloud.cos.http.HttpMethodName;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.client.WebClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.HmacUtils;

import java.util.Calendar;
import java.util.Date;

/**
 * 这个测试方法没有彻底完成，暂时搁置.....
 */
@RunWith(VertxUnitRunner.class)
public class RestVertx {

    /**
     * 使用rest方式请求，需要进行签名操作，然后在请求头添加签名字符串
     *
     * 注意：Rest API这一块不熟悉
     * @throws InterruptedException
     */
    @Test
    public void restApi() throws InterruptedException {
        Vertx vertx=Vertx.vertx();


        COSSigner cosSigner=new COSSigner();
        String srcPath="<yourBucketName-APPID>.cos.<Region>.myqcloud.com";
        String Authorization=cosSigner.buildAuthorizationStr(HttpMethodName.GET,srcPath,new BasicCOSCredentials("","",""),new Date());
//        String Authorization=signature();

        WebClient client = WebClient.create(vertx);
        client
                .get( "<yourBucketName-APPID>.cos.<Region>.myqcloud.com", "/")
                .putHeader("Authorization",Authorization)
                .send(ar -> {
                    if (ar.succeeded()) {
                        HttpResponse<Buffer> response = ar.result();
                        System.out.println("Received response with status code" + response.statusCode());
                        System.out.println(response.body());
                    } else {
                        System.out.println("Something went wrong " + ar.cause().getMessage());
                    }
                });

        Thread.sleep(10000);
    }

    /**
     * Signature 的计算分为四个步骤：
     *
     *      SignKey = HMAC-SHA1(SecretKey,"[q-key-time]")
     *      HttpString = [HttpMethod]\n[HttpURI]\n[HttpParameters]\n[HttpHeaders]\n
     *      StringToSign = [q-sign-algorithm]\n[q-sign-time]\nSHA1-HASH(HttpString)\n
     *      Signature = HMAC-SHA1(SignKey,StringToSign)
     *
     * 注意：可能某个地方有问题
     */
    private String signature() {
        //1.对签名的有效起止时间加密计算值 SignKey。
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        String timeRange=calendar.getTimeInMillis()+":";
        calendar.add(Calendar.MINUTE,10);
        timeRange=timeRange+":"+calendar.getTimeInMillis();
        System.out.println("timeRange="+timeRange);

        String signKey = HmacUtils.hmacSha1Hex("YourSecretKey", timeRange);
        System.out.println("signKey="+signKey);

        //2.根据固定格式组合生成 HttpString。
        String HttpString ="get\n\'/\n\nhost=<yourBucketName-APPID>.cos.<Region>.myqcloud.com\n";
        System.out.println("first HttpString="+HttpString);

        HttpString=DigestUtils.sha1Hex(HttpString);
        System.out.println("second HttpString="+HttpString);

        //3.加密 HttpString，并根据固定格式组合生成 StringToSign。
        String StringToSign="sha1\n"+timeRange+"\n"+HttpString+"\n";
        System.out.println("StringToSign="+StringToSign);

        //4.加密 StringToSign，生成 Signature。
        String Signature=HmacUtils.hmacSha1Hex(signKey,StringToSign);
        System.out.println("Signature="+Signature);
        return Signature;
    }

}
